package simpleftp.client.control;

import java.io.File;


public class JsonTree {
	
	//True to active the Json and false for the file serialization
	public static boolean actif = true;
	
	public static String toJson(File file, int level) {
		
        File[] files = file.listFiles();
        StringBuffer sb = null;
        //If there is files 
        if (files != null && files.length > 0) {
        	
            sb = new StringBuffer();
            
           if(level==1) 
        	   {
        	   		sb.append("{\n\t \"name\": \"").append(file.getName()).append("\",");
        	   }
           
           sb.append("\n");
           for(int i=0;i<level;i++)
           	sb.append("\t");
           
           sb.append("\"folder\": [");
           
            for (int y=0;y<files.length;y++) {  
                
            	File f = files[y];
            	
                for(int i=0;i<level;i++)
                	sb.append("\t");
                
                sb.append("{\n");
                
                for(int i=0;i<level;i++)
                	sb.append("\t");
                
                sb.append("\"name\" :\"").append(f.getName());
                
                if (f.isDirectory()) {
                	sb.append("\",");
                	
                	sb.append(toJson(f,level+1));
                	
                } 
                else
                	sb.append("\"");
                  
                for(int i=0;i<level;i++)
                	sb.append("\t");
                
                	sb.append("}");
                	
                if(y!=(files.length-1))
                	sb.append(",\n");
                
            }
            
            for(int i=0;i<level;i++)
               	sb.append("\t");
            
            sb.append("\n]");
            
            if(level==1) sb.append("\n}");
            
            return sb.toString();
            
        } 
        else
        {
        	return("\"folder\":[ {\"name\" : \"\"}]");
        }
    }

}
